const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isShowAgreement: false,
    signRelation: null,
    relation_index: -1,
    relation: [
      { value: 1, name: '爸爸' },
      { value: 2, name: '妈妈' },
      { value: 3, name: '亲戚' },
      { value: 4, name: '朋友' }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var productId = options.productId;
    var groupNo = options.groupNo;
    this.setData({
      productId: productId,
      groupNo: groupNo ? groupNo : null
    })
    this.loadProduct(productId);
    this.queryGroupOrderDetail();
  },
  bindPickerChange_relation: function (e) {
    var relation_index = this.data.relation_index;
    var signRelation = parseInt(e.detail.value) + 1;
    for (var i in this.data.relation) {
      if (this.data.relation[i].value == signRelation) {
        relation_index = i;
        break;
      }
    }
    this.setData({
      signRelation: signRelation,
      relation_index: relation_index
    })
  },
  formSubmit: function (e) {
    console.log(e.detail.formId)
    let params = {
      productId: this.data.productId,
      groupNo: this.data.groupNo,
      channel: 'WeiXin',
      signWay: 'group',
      formId: e.detail.formId,
      signRelation: e.detail.value.signRelation,
      emerMobile: e.detail.value.emerMobile,
      childName: e.detail.value.childName
    }
    if (!params.childName) {
      wx.showToast({title: '小孩姓名不能为空', icon: 'none', duration: 1000});
      return;
    }
    if (!params.emerMobile) {
      wx.showToast({title: '联系电话不能为空', icon: 'none', duration: 1000 });
      return;
    }
    if (!params.signRelation) {
      wx.showToast({title: '请选择您与小孩的关系', icon: 'none', duration: 1000 });
      return;
    }
    this.pay(params);
  },
  pay: function (params) {
    if (this.data.orderNo != null) {
      this.paySign(this.data.orderNo);
    } else {
      this.createSign(params);
    }
  },
  createSign: function (data) {
    let that = this;
    // 下单接口
    wx.request({
      url: app.httpUrl + '/course/order/createSign',
      header: {
        'xcx-xtoken': app.globalData.openid,
        'content-type': 'application/json' // 默认值
      },
      data: data,
      method: "POST",
      success: (res) => {
        var signData = res.data.data;
        if (res.data.code != '000') {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          });
          return;
        }
        that.setData({
          orderNo: signData.orderNo
        });
        this.payment(signData);
      }
    })
  },
  paySign: function (orderNo) {
    wx.request({
      url: app.httpUrl + '/pay/sign',
      header: {
        'xcx-xtoken': app.globalData.openid,
        'content-type': 'application/json' // 默认值
      },
      data: {
        orderNo: orderNo
      },
      method: "POST",
      success: (res) => {
        var signData = res.data.data;
        this.payment(signData);
      }
    })
  },
  payment: function (signData) {
    var that = this;
    wx.requestPayment({
      'timeStamp': signData.timeStamp,
      'nonceStr': signData.nonceStr,
      'package': signData.packages,
      'signType': 'MD5',
      'paySign': signData.paySign,
      success: function (res) {
        console.log(res)
        that.setData({
          orderNo: null
        });
        wx.showToast({
          title: '支付成功',
          icon: 'success',
          duration: 2000
        });
        wx.navigateTo({
          url: '/pages/groupOrderDetail/groupOrderDetail?pay=apply&orderNo=' + signData.orderNo,
          success: function () {
            console.log('success');
          }
        })
      },
      fail: function (res) {
        //wx.showToast({ title: '支付失败', icon: 'none', duration: 2000 });
        wx.navigateTo({
          url: '/pages/groupOrderDetail/groupOrderDetail?orderNo=' + signData.orderNo,
          success: function () {

          }
        })
      },
      'complete': function (res) {
        console.log(res);
      }
    })
  },
  loadProduct: function (productId) {
    let that = this;
    // 课程介绍接口
    wx.request({
      url: app.httpUrl + '/course/queryCourseDetail/' + productId,
      header: {
        'xcx-xtoken': app.globalData.openid,
        'content-type': 'application/json' // 默认值
      },
      method: "GET",
      success: (res) => {
        that.setData({
          course: res.data.data
        })
      }
    })
  },
  queryGroupOrderDetail: function() {
    if (this.data.groupNo == null) return;
    let that = this;
    // 课程介绍接口
    wx.request({
      url: app.httpUrl + '/course/queryGroupOrderDetail/' + this.data.groupNo,
      header: {
        'xcx-xtoken': app.globalData.openid,
        'content-type': 'application/json' // 默认值
      },
      method: "GET",
      success: (res) => {
        that.setData({
          groupOrder: res.data.data
        })
      }
    })
  }, 
  agreementTab: function() {
    this.setData({
      isShowAgreement: this.data.isShowAgreement ? false : true
    })
  }
})